package com.mixes.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;

/**
 * 画像処理関係のUtilityクラス
 * @author $Author:$
 * @version $Revision:$
 */
public class ImageUtility {
    public static Bitmap getBitmapFromURL(String src) {
        try {
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (IOException e) {
        }
        return null;
    }

    /**
     * byte型をBitmap型に変換
     * @param bytes
     * @return
     */
    public static Bitmap getBitmap(byte[] bytes) {
        Bitmap bitmap = null;
        if (bytes != null) {
            bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
        }
        return bitmap;
    }

    /**
     * UriからBitmapを取得
     * @param context　Context
     * @param uri　画像までのUri
     * @return Bitmap
     */
    public static Bitmap getBitmap(Context context, String picUri) {
        try {
            Uri uri = Uri.fromFile(new File(picUri));
            return getBitmap(context, uri);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * UriからBitmapを取得
     * @param context　Context
     * @param uri　画像までのUri
     * @return Bitmap
     */
    public static Bitmap getBitmap(Context context, Uri uri) {
        try {
            InputStream is = context.getContentResolver().openInputStream(uri);
            Bitmap img = BitmapFactory.decodeStream(is);
            is.close();
            return img;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * byte型をDrabable型に変換
     * @deprecated 非推奨メソッド
     * @param bytes
     * @return
     */
    public static Drawable getDrawable(byte[] bytes) {
        if (bytes == null) {
            return null;
        }
        Drawable image = null;
        image = new BitmapDrawable(getBitmap(bytes));
        return image;
    }

    /**
     * Bitmap型をbyte型に変換
     * @param bitmap
     * @return
     */
    public static byte[] convertToStream(Bitmap bitmap) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream)) {
            return stream.toByteArray();
        }
        return null;

    }

    /**
     * Drawable型をbyte型に変換
     * @param d
     * @return
     */
    public static byte[] convertToStream(Drawable d) {
        if (d == null) {
            return null;
        }
        BitmapDrawable bitDw = ((BitmapDrawable) d);
        Bitmap bitmap = bitDw.getBitmap();
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream)) {
            return stream.toByteArray();
        }
        return null;
    }
}
